package cn.ulc.socket.admin.controller;


import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.annotation.IgnoreAuth;
import cn.ulc.common.exception.PlatformException;
import cn.ulc.common.model.param.FileParam;
import cn.ulc.common.util.AliyunOssFileUtil;
import cn.ulc.common.util.Result;
import cn.ulc.socket.admin.service.common.CommonServ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping(value = "/common")
public class CommonCtrl {
    private static final Log log = LogFactory.get();

    @Autowired
    CommonServ commonServ;

    /**
     * 上传 图片 ，音频 等文件
     * @param file
     * @return
     */
    @IgnoreAuth
    @RequestMapping(value = "/uploadFile",method = RequestMethod.POST)
    public Result uploadFile(@RequestParam MultipartFile file){
        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        String realPath = "";
        fileName = IdUtil.randomUUID() + suffixName;
        try {
            realPath = AliyunOssFileUtil.uploadPublicFile(file.getInputStream(),fileName,file.getSize());
            if(StrUtil.isBlank(realPath)) new PlatformException("图片上传失败 ~ ~");
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.success(realPath,true);
    }

    /**
     * 上传 图片 ，音频 等文件基于Base64
     * @param data
     * @return
     */
    @IgnoreAuth
    @RequestMapping(value = "/uploadFileBase64",method = RequestMethod.POST)
    public Result uploadFileBase64(String data){
        if(StrUtil.isBlank(data)) return Result.fail("上传文件不存在!");
        return Result.success(commonServ.uploadFileBase64(data),true);
    }

    /**
     * 文件下载
     * @param param
     * @param request
     * @param response
     */
    @IgnoreAuth
    @RequestMapping(value = "/downLoadFile", method = RequestMethod.GET)
    public void downLoadFile(@Validated FileParam param, HttpServletRequest request, HttpServletResponse response) {
        AliyunOssFileUtil.downFileBase64(request,response, param);
    }

    /**
     * 文件下载Base64
     * @param param
     * @param request
     * @param response
     */
    @IgnoreAuth
    @RequestMapping(value = "/downLoadFileBase64", method = RequestMethod.GET)
    public Result downLoadFileBase64(@Validated FileParam param, HttpServletRequest request, HttpServletResponse response) {
        String result = AliyunOssFileUtil.getFileBase64(request,response,param);
        return Result.success(result,true);
    }
}
